package com.the9.C9Alarm;

import holidaynotifaction.HolidayReceiver;
import android.R.integer;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {
	Context c;

	
	NotificationManager nfManger;

	@Override
	public void onReceive(Context context, Intent intent) {
		this.c = context;
		Log.e("AlarmReceiver", "receive_action");
		nfManger =(NotificationManager)context.getSystemService(Activity.NOTIFICATION_SERVICE);
		// Toast.makeText(context, "This the time", Toast.LENGTH_LONG).show();
		if (intent.getAction().equals(Symbol.ACTION_DONOTHING)) {
			// do nothing now
			((NotificationManager) context
					.getSystemService(Activity.NOTIFICATION_SERVICE)).cancel(0);

		} else if (intent.getAction().equals(Symbol.ACTION_RECEIVE_ALARM)) {
			nfManger.cancel(Symbol.NOTIFICATION_MESSAGE);
			
			Intent i = new Intent(context, AlarmReceiveActivity.class);
			int index = intent.getIntExtra("alarm_index", -1);
			Log.e("receive_index", "" + index);
			i.putExtra("alarm_index", index);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

			context.startActivity(i);
			((NotificationManager) context
					.getSystemService(Activity.NOTIFICATION_SERVICE))
					.cancel(-1);

		} else if (intent.getAction().equals(Symbol.ACTION_LOSE)) {
			nfManger = (NotificationManager) c
					.getSystemService(Activity.NOTIFICATION_SERVICE);
			stillSleep();

		} else if (intent.getAction().equals(Symbol.ACTION_WIN)) {
			nfManger = (NotificationManager) c
					.getSystemService(Activity.NOTIFICATION_SERVICE);
			awake();
		}else if (intent.getAction().equals(Symbol.ACTION_GIVEUP)) {
			nfManger = (NotificationManager) c
					.getSystemService(Activity.NOTIFICATION_SERVICE);
			giveup();
		}else if (intent.getAction().equals(Symbol.ACTION_START_MAIN)) {
			Intent i = new Intent(context, SplashScreen.class);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
			Log.e("AlarmReceiver", "start_main_action");
		} else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			AlarmSet.setAlarm(c);
			HolidayReceiver.setAlarm(c);
		} else if (intent.getAction().equals(Symbol.ACTION_START)) {
			Media.getInstance(context).Stop();
		}else {
			Log.e("AlarmReceiver", "unknow Action");
		}
	}

	Intent nextIntent;

	private synchronized void awake() {
		Utils.GameOver();
		int time = Utils.finishSleepTime(c);

		boolean isLaiChuang = true;
		if (time == 0) {
			isLaiChuang = false;
		}

		Bundle bd = new Bundle();
		bd.putBoolean(c.getString(R.string.isLaiChuang), isLaiChuang);
		bd.putInt(c.getString(R.string.awakeTime), time);
		nextIntent = new Intent(c, ShareToFriendActivity.class);
		nextIntent.putExtra(c.getString(R.string.sleepState), bd);
		nextIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		AlarmProvider.geInstance(c).disableSleepAlarm(0);
		nameIsToHard();

		
	}

	private synchronized void nameIsToHard() {
		Utils.GameReset();
		// isOver = true;
		nfManger.cancel(Symbol.NOTIFICATION_RINGING);
		AlarmSet.setAlarm(c);

		Media.getInstance(c).Stop();
		Intent i = new Intent();
		i.setAction(Symbol.ACTION_DONOTHING);
		i.setClass(c, AlarmReceiver.class);

		if (nextIntent != null) {

			c.startActivity(nextIntent);

		}

	}

	private synchronized void stillSleep() {
		AlarmProvider ap=AlarmProvider.geInstance(c);
		AlarmBean ab = ap.getAlarm(ap.getCurrendIndex());
		ab.setDay(Symbol.DAY_ONCE);// sym
		ab.setInterval(ab.getSleepDelay() * 1000 * 60l);// sym
		ab.setTime(System.currentTimeMillis() + ab.getInterval());
		ab.setIndex(0);// sleepalarm ,start from 0
		// ab.setName("10fenyan");
		ab.setEnabled(true);

		AlarmProvider.geInstance(c).setSleepAlarm(ab);
		nameIsToHard();
	}
	
	private synchronized void giveup() {
		Utils.finishSleepTime(c);
		int time = Integer.MAX_VALUE;
		Utils.setLaichuangTime(time);
		Utils.GameReset();
		

		
		nextIntent = new Intent(c, ShareToFriendActivity.class);
	//	nextIntent.putExtra(c.getString(R.string.sleepState), bd);
		nextIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		AlarmProvider.geInstance(c).disableSleepAlarm(0);
		
		nameIsToHard();
		//nfManger.cancel(Symbol.NOTIFICATION_RINGING);
	}
}